/**
 * 作者:李杰
 * email: maillijie@gmail.com
 */
package com.javainfo.actions.admin;

import org.apache.struts2.rest.DefaultHttpHeaders;
import org.apache.struts2.rest.HttpHeaders;

import com.javainfo.entity.User;
import com.javainfo.exception.AppException;
import com.javainfo.util.JavaInfoActionSupport;

public class UserCtrl extends JavaInfoActionSupport {

	/** serialVersionUID */
	private static final long serialVersionUID = 1L;

	public HttpHeaders create() {
		// 获取画面输入的值，转换成service层需要的Dto
		String userAccount = getRequest().getParameter("userAccount");
		User user = new User();
		user.setUserAccount(userAccount);

		// 判断权限并执行
		getServiceFactory().getUserService().add(user);
		addActionMessage("新建用户成功!");
		setRedirectUrl("admin/anyone/userManage");
		return new DefaultHttpHeaders("urlhandler");
	}

	@SuppressWarnings("finally")
	public HttpHeaders destroy() {
		User user = new User();
		user.setId(Integer.valueOf(getId()));
		// 判断权限并执行
		try {
			getServiceFactory().getUserService().delete(user);
			addActionMessage("删除用户成功!");
		} catch (AppException e) {
			addActionError(e.getMessage());
		} finally {
			setRedirectUrl("admin/anyone/userManage");
			return new DefaultHttpHeaders("urlhandler");
		}
	}
}
